import {createRouter, createWebHashHistory, type Router,RouteLocationNormalized} from "vue-router"
import pinia from "../stores/store"
import {userMenuStore} from "../stores/menu"
import { useTitle } from '@vueuse/core'
import {RouterList,defaultUrl} from "./routerList"
// TODO 注释代码是单层路由，不适应多层。可以改成动态多层路由，现在手动维护的RouterList
// const RouterList:Array<RouteRecordRaw> = []
// for (const path in requireRouter){
//   const name = path.match(/.*\/(.+)\/index.vue$/)
//   const routerName =  name? name[1] : ""
//   RouterList.push({
//     name: routerName.toLowerCase(),
//     path: `/${routerName.toLowerCase()}`,
//     component: requireRouter[path],
//   })
// }

const menuStore = userMenuStore(pinia)
menuStore.SET_MENU(RouterList)
const router:Router = createRouter({
  history: createWebHashHistory(),
  routes: RouterList.concat([{path: "/",redirect: defaultUrl}])
})

router.afterEach((to:RouteLocationNormalized)=>{
  const name = (to.name || '' ) as string
  useTitle('YsCrud | ' + name)
})

export default router
